<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Popcorn.Core - Unit tests</title>
    <link rel="stylesheet" href="test.css">
		<script type="text/javascript" src="../src/core.js"></script>
		<script type="text/javascript" src="test.js"></script>
	</head>
	<body>
		<h3>Popcorn.Core - unit tests</h3>

		<p>
      <div><b>Tests run: <span id="tests_count"></span></b></div>
      <div><b>Tests passed: <span id="tests_passed"></span></b></div>
      <div><b>Tests failed: <span id="tests_failed"></span></b></div>
    </p>

		<p>
		<div><b>Core lib</b></div>
        <script type="text/javascript">
          with(Popcorn.Core) {
            assertGen(gen("abc"), null, "abc", "gen()");
            assertGen(chain(gen("a"), function(a) { return gen(a + "bc"); }), null, "abc", "chain()");
            assertGen(seq([gen("a"), gen("b")], cConcat), null, ["a", "b"], "seq(cConcat)");
            assertGen(seq([gen("a"), gen("b")], cJoin), null, "ab", "seq(cJoin)");
            assertGen(replicate(gen("a"), cJoin)(3), null, "aaa", "replicate(cJoin)");
            assertGen(repeat(3, 'a'), null, ['a','a','a'], "repeat(value)");
            assertGen(repeat(3, gen('a')), null, ['a','a','a'], "repeat(function)");
            assertGen(repeat(3, [gen('a')]), null, ['a','a','a'], "repeat(array)");
		  	assert(permutate(array(repeat(3, 'a')), {}), [['a','a','a']], "array()");
          }
		</script>
		</p>

		<p>
		<div><b>Object generators</b></div>
        <script type="text/javascript">
          with(Popcorn.Core) {
            var d     = { name : "John", age : 35 };
                name  = property("name"),
                age   = property("age"),
                mike  = name(gen("Mike")),
                john  = name(gen("John")),
                age40 = age(gen(40));
            assertGen(mike, d, { name : "Mike" }, "property()");
            assertGen(update([mike, age40]), d, { name : "Mike", age : 40 }, "update()");
            assertGen(mutate([gen("Mike"), gen("John")]), {}, ["Mike", "John"], "mutate()");

            var g = { name : gen("Mike"), age : gen(40) };
            assert(permutate(g, d), 
                [{ name : "Mike", age : 40}], "permutate()");
          }
		</script>
		</p>

		<p>
		<div><b>State and variables</b></div>
        <script type="text/javascript">
          with(Popcorn.Core) {
            var b = { att1: 'att1' },
                g = { att1: setVar('var1', gen('att1-1')) };

            var r = permutate(g, b, -1, {}, id);
            assert(r.result[0].att1, 'att1-1', 'setVar(result)');
            assert(r.state.var1, 'att1-1', 'setVar(state)');

            g = { 
              att1: setVar('var1', gen(10)),
              att2: withVar('var1', function(v) { return gen(v + 5); }),
              att3: varGen('var1')
            };
            var r = permutate(g, b, -1, {}, id);
            assert(r.result[0].att2, 15, 'withVar()');
            assert(r.result[0].att3, 10, 'varGen()');

            g = { 
              att1: withState(function(s) { 
                s.var1 = 'var1';
                return gen('att1-1'); })
              };
            var r = permutate(g, b, -1, {}, id);
            assert(r.state.var1, 'var1', 'withState()');
          }

		</script>
		</p>

		<p>
		<div><b>Random generators</b></div>
        <script type="text/javascript">
          with(Popcorn.Core) {
            var rand = random(12345);
            var seedTest = [rand.int()().result, rand.int()().result, rand.int()().result];
            assertGen(repeat(3, random(12345).int()), null, seedTest, "random(seed)");
            assertGen(rand.int(1,5), null, function(r) { return (r >= 1) && (r <= 5); }, "int()");
            assertGen(rand.int(1,1), null, 1, "int(1,1)");
            assertGen(random().int(1,1), null, 1, "random().int()");
            assertGen(rand.element(['a']), null, 'a', "elemetn(one element)");
            assertGen(rand.element(['a', 'b', 'c']), null, function(r) { return "abc".indexOf(r) >= 0; }, 
                "elemetn(array)");
          }
		</script>
		</p>

		<p>
		<div><b>Utils lib</b></div>
      <script type="text/javascript">
          with(Popcorn.Core) {
            assertTrue(isNull(null), "isNull(null)");
            assertFalse(isNull(undefined), "not isNull(undefined)");
            assertTrue(isUndefined(undefined), "isUndefined(undefined)");
            assertFalse(isUndefined(null), "not isUndefined(null)");
            assertTrue(isFunction(function() { return ""; }), "isFunction()");
            assertTrue(isDate(new Date()), "isDate(now)");
            assertFalse(isDate("Sep 13, 2009"), "isDate(date string)");
            assertFalse(isDate(123456), "isDate(numb)");
            assertTrue(isObject({}), "isObject({})");
            assertFalse(isObject(function() { return ""; }), "not isFunction(<function>)");
            assertTrue(isInt(2), "isInt(2)");
            assertFalse(isInt(2.1), "isInt(2.1)");

            // test map()
              var plus1 = function(x) { return x + 1; };
            assert([2,3,4], map(plus1, [1,2,3]), "map()");

            // test merge()
            var o1 = { p1 : 'o1' },
                o2 = { p2 : 'o2' },
                o  = merge(o1, o2);
            assertGen(gen(o), null, function(r) { return (r.p1 === "o1") && (r.p2 === "o2"); }, "merge()");
          }
		</script>
		</p>

	</body>
</html>

